﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.IDAL;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:Products
	/// </summary>
	public partial class Products:IProducts
	{
		public Products()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("ProductId", "Products"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ProductId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Products");
			strSql.Append(" where ProductId=@ProductId ");
			SqlParameter[] parameters = {
					new SqlParameter("@ProductId", SqlDbType.Int,4)			};
			parameters[0].Value = ProductId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Maticsoft.Model.Products model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Products(");
			strSql.Append("ProductId,ProductName,ProductType,Description,InterestRate,StartDate,EndDate,MinimumInvestment,MaximumInvestment,CurrentValue,InvestmentReturn)");
			strSql.Append(" values (");
			strSql.Append("@ProductId,@ProductName,@ProductType,@Description,@InterestRate,@StartDate,@EndDate,@MinimumInvestment,@MaximumInvestment,@CurrentValue,@InvestmentReturn)");
			SqlParameter[] parameters = {
					new SqlParameter("@ProductId", SqlDbType.Int,4),
					new SqlParameter("@ProductName", SqlDbType.VarChar,255),
					new SqlParameter("@ProductType", SqlDbType.VarChar,255),
					new SqlParameter("@Description", SqlDbType.VarChar,255),
					new SqlParameter("@InterestRate", SqlDbType.VarChar,255),
					new SqlParameter("@StartDate", SqlDbType.DateTime,8),
					new SqlParameter("@EndDate", SqlDbType.DateTime2,8),
					new SqlParameter("@MinimumInvestment", SqlDbType.Decimal,9),
					new SqlParameter("@MaximumInvestment", SqlDbType.Decimal,9),
					new SqlParameter("@CurrentValue", SqlDbType.Decimal,9),
					new SqlParameter("@InvestmentReturn", SqlDbType.Decimal,9)};
			parameters[0].Value = model.ProductId;
			parameters[1].Value = model.ProductName;
			parameters[2].Value = model.ProductType;
			parameters[3].Value = model.Description;
			parameters[4].Value = model.InterestRate;
			parameters[5].Value = model.StartDate;
			parameters[6].Value = model.EndDate;
			parameters[7].Value = model.MinimumInvestment;
			parameters[8].Value = model.MaximumInvestment;
			parameters[9].Value = model.CurrentValue;
			parameters[10].Value = model.InvestmentReturn;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.Products model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Products set ");
			strSql.Append("ProductName=@ProductName,");
			strSql.Append("ProductType=@ProductType,");
			strSql.Append("Description=@Description,");
			strSql.Append("InterestRate=@InterestRate,");
			strSql.Append("StartDate=@StartDate,");
			strSql.Append("EndDate=@EndDate,");
			strSql.Append("MinimumInvestment=@MinimumInvestment,");
			strSql.Append("MaximumInvestment=@MaximumInvestment,");
			strSql.Append("CurrentValue=@CurrentValue,");
			strSql.Append("InvestmentReturn=@InvestmentReturn");
			strSql.Append(" where ProductId=@ProductId ");
			SqlParameter[] parameters = {
					new SqlParameter("@ProductName", SqlDbType.VarChar,255),
					new SqlParameter("@ProductType", SqlDbType.VarChar,255),
					new SqlParameter("@Description", SqlDbType.VarChar,255),
					new SqlParameter("@InterestRate", SqlDbType.VarChar,255),
					new SqlParameter("@StartDate", SqlDbType.DateTime2,8),
					new SqlParameter("@EndDate", SqlDbType.DateTime2,8),
					new SqlParameter("@MinimumInvestment", SqlDbType.Decimal,9),
					new SqlParameter("@MaximumInvestment", SqlDbType.Decimal,9),
					new SqlParameter("@CurrentValue", SqlDbType.Decimal,9),
					new SqlParameter("@InvestmentReturn", SqlDbType.Decimal,9),
					new SqlParameter("@ProductId", SqlDbType.Int,4)};
			parameters[0].Value = model.ProductName;
			parameters[1].Value = model.ProductType;
			parameters[2].Value = model.Description;
			parameters[3].Value = model.InterestRate;
			parameters[4].Value = model.StartDate;
			parameters[5].Value = model.EndDate;
			parameters[6].Value = model.MinimumInvestment;
			parameters[7].Value = model.MaximumInvestment;
			parameters[8].Value = model.CurrentValue;
			parameters[9].Value = model.InvestmentReturn;
			parameters[10].Value = model.ProductId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ProductId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Products ");
			strSql.Append(" where ProductId=@ProductId ");
			SqlParameter[] parameters = {
					new SqlParameter("@ProductId", SqlDbType.Int,4)			};
			parameters[0].Value = ProductId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string ProductIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Products ");
			strSql.Append(" where ProductId in ("+ProductIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.Products GetModel(int ProductId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ProductId,ProductName,ProductType,Description,InterestRate,StartDate,EndDate,MinimumInvestment,MaximumInvestment,CurrentValue,InvestmentReturn from Products ");
			strSql.Append(" where ProductId=@ProductId ");
			SqlParameter[] parameters = {
					new SqlParameter("@ProductId", SqlDbType.Int,4)			};
			parameters[0].Value = ProductId;

			Maticsoft.Model.Products model=new Maticsoft.Model.Products();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.Products DataRowToModel(DataRow row)
		{
			Maticsoft.Model.Products model=new Maticsoft.Model.Products();
			if (row != null)
			{
				if(row["ProductId"]!=null && row["ProductId"].ToString()!="")
				{
					model.ProductId=int.Parse(row["ProductId"].ToString());
				}
				if(row["ProductName"]!=null)
				{
					model.ProductName=row["ProductName"].ToString();
				}
				if(row["ProductType"]!=null)
				{
					model.ProductType=row["ProductType"].ToString();
				}
				if(row["Description"]!=null)
				{
					model.Description=row["Description"].ToString();
				}
				if(row["InterestRate"]!=null)
				{
					model.InterestRate=row["InterestRate"].ToString();
				}
					//model.StartDate=row["StartDate"].ToString();
					//model.EndDate=row["EndDate"].ToString();
				if(row["MinimumInvestment"]!=null && row["MinimumInvestment"].ToString()!="")
				{
					model.MinimumInvestment=decimal.Parse(row["MinimumInvestment"].ToString());
				}
				if(row["MaximumInvestment"]!=null && row["MaximumInvestment"].ToString()!="")
				{
					model.MaximumInvestment=decimal.Parse(row["MaximumInvestment"].ToString());
				}
				if(row["CurrentValue"]!=null && row["CurrentValue"].ToString()!="")
				{
					model.CurrentValue=decimal.Parse(row["CurrentValue"].ToString());
				}
				if(row["InvestmentReturn"]!=null && row["InvestmentReturn"].ToString()!="")
				{
					model.InvestmentReturn=decimal.Parse(row["InvestmentReturn"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ProductId,ProductName,ProductType,Description,InterestRate,StartDate,EndDate,MinimumInvestment,MaximumInvestment,CurrentValue,InvestmentReturn ");
			strSql.Append(" FROM Products ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ProductId,ProductName,ProductType,Description,InterestRate,StartDate,EndDate,MinimumInvestment,MaximumInvestment,CurrentValue,InvestmentReturn ");
			strSql.Append(" FROM Products ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Products ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ProductId desc");
			}
			strSql.Append(")AS Row, T.*  from Products T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Products";
			parameters[1].Value = "ProductId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

